<template>
  <el-form :model="detailForm" ref="detailForm" label-position="right">

    <el-card class="box-card" shadow="never">
      <div slot="header" class="header-card">
        <span class="header-card-title">GPS续费预警配置</span>
      </div>
      <el-row>
        <el-col :span="24" class="col-wrap" v-for="(item,index) in detailForm.rows" :key="index"
                v-if="item.warnType === '1'">
          <el-switch v-model="item.configSwitch" active-value="1" inactive-value="0"/>
          <el-form-item label-width="200px" label="GPS到期时间提前预警规则:" class="text-day"
                        :prop="`rows.${index}.warnSustainDays`"
                        :rules="{required: true, message: '请选择GPS到期时间提前预警规则',trigger: 'change'}">
            <el-select filterable v-model="item.warnSustainDays" clearable style="width: 100%">
              <el-option v-for="item in dict.type.gps_warn_days_config" :label="item.label" :value="item.value"
                         :key="item.value"/>
            </el-select>
          </el-form-item>
          <span class="text-day">天</span>
        </el-col>
      </el-row>
    </el-card>


    <el-card class="box-card" shadow="never" style="margin-bottom: 0">
      <div slot="header" class="header-card">
        <span class="header-card-title">GPS排查预警配置</span>
      </div>
      <el-row v-for="(item,index) in detailForm.rows" :key="index" v-if="item.warnType !== '1'" style="margin-bottom: 10px">

        <el-col :span="24" class="col-wrap" v-if="item.warnType === '2'">
          <el-switch v-model="item.configSwitch" active-value="1" inactive-value="0"/>

          <el-form-item label-width="115px" label="报警类型:" class="text-day"
                        :prop="`rows.${index}.warnSonType`"
                        :rules="{required: true, message: '请选择报警类型', trigger: 'change', }">
            <el-select placeholder="请选择报警类型" v-model="item.warnSonType"
                       @change="handleSelect(item,dict.type.gps_warn_config)"
                       clearable>
              <template v-for="dict in dict.type.gps_warn_config">
                <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
              </template>
            </el-select>
          </el-form-item>

          <el-form-item label-width="130px" label="预警时间:" class="text-day"
                        :prop="`rows.${index}.warnSustainDays`"
                        :rules="{required: true, message: '请输入预警时间', trigger: 'blur',}">
            <el-input v-model="item.warnSustainDays" @change="handleDay(item,'warnSustainDays')"
                      placeholder="请输入预警时间"/>
          </el-form-item>
          <span class="text-day">天</span>

          <el-form-item label-width="130px" label="未处理推消息:" class="text-day">
            <el-select v-model="item.earlyWarn" multiple @change="handleMultiple(item)"
                       clearable style="width: 100%">
              <el-option v-for="item in dict.type.gps_warn_days_config" :label="item.label" :value="item.value"
                         :key="item.value"/>
            </el-select>
          </el-form-item>
          <span class="text-day">天</span>

          <i class="el-icon-circle-plus-outline icon_add"
             v-if="detailForm.rows[index + 1].warnType === '3'"
             @click="add(index, 0)"></i>
          <i v-else class="el-icon-remove-outline icon_add" @click="sub(index, 0)"></i>
        </el-col>

        <!--   GPS状态     -->
        <el-col :span="24" class="col-wrap" v-if="item.warnType === '3'">
          <el-switch v-model="item.configSwitch" active-value="1" inactive-value="0"/>

          <el-form-item label-width="115px" label="GPS状态:" class="text-day"
                        :prop="`rows.${index}.warnSonType`"
                        :rules="{ required: true,message: '请选择GPS状态', trigger: 'change',}">
            <el-select placeholder="请选择GPS状态" filterable
                       @change="handleSelect(item,dict.type.gps_off_line_config)"
                       v-model="item.warnSonType" clearable>
              <el-option v-for="item in dict.type.gps_off_line_config"
                         :label="item.label" :value="item.value" :key="item.value"/>
            </el-select>
          </el-form-item>

          <el-form-item label-width="130px" label="状态持续时长:" class="text-day"
                        :prop="`rows.${index}.warnSustainDays`"
                        :rules="{ required: true, message: '请输入状态持续时长', trigger: 'blur',}">
            <el-input v-model="item.warnSustainDays" @change="handleDay(item,'warnSustainDays')"
                      placeholder="请输入状态持续时长"/>
          </el-form-item>
          <span class="text-day">天</span>

          <el-form-item label-width="130px" label="未处理推消息:" class="text-day">
            <el-select multiple v-model="item.earlyWarn"
                       @change="handleMultiple(item)"
                       clearable style="width: 100%">
              <el-option v-for="item in dict.type.gps_warn_days_config" :label="item.label" :value="item.value"
                         :key="item.value"/>
            </el-select>
          </el-form-item>
          <span class="text-day">天</span>

          <i class="el-icon-circle-plus-outline icon_add"
             v-if="!detailForm.rows[index + 1]"
             @click="add(index, 1)"></i>
          <i v-else class="el-icon-remove-outline icon_add" @click="sub(index, 1)"></i>
        </el-col>

      </el-row>
    </el-card>

  </el-form>
</template>

<script>


export default {
  name: "GPSWarning",
  dicts: ["gps_warn_config", "gps_off_line_config", "gps_warn_days_config"],
  props: {
    value: {
      type: Object,
      default: {

      }
    }
  },

  computed: {
    detailForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key, value) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            that.$emit("input", {...target, [key]: value});
            return true;
          },
        });
        return proxy;
      },
    },
  },
  data() {
    return {};
  },

  methods: {
    add(index, type) {
      if (0 === type) {
        let val = {
          warnType: "2",
          warnSonType: "",
          warnSonTypeStr: "",
          configSwitch: "",
          warnSustainDays: "",
          earlyWarn: [],
          earlyWarnDays: "",
        }
        this.detailForm.rows.splice(index + 1, 0, val);
      } else if (1 === type) {
        let val = {
          warnType: "3",
          warnSonType: "",
          warnSonTypeStr: "",
          configSwitch: "",
          warnSustainDays: "",
          earlyWarn: [],
          earlyWarnDays: "",
        }
        this.detailForm.rows.splice(index + 1, 0, val);
      }
      this.$forceUpdate();
    },

    sub(index, type) {
      if (0 === type) {
        this.detailForm.rows.splice(index, 1);
      } else if (1 === type) {
        this.detailForm.rows.splice(index, 1);
      }
      this.$forceUpdate();
    },

    handleDay(val, key) {
      if (!val[key]) return
      const regex = /^[1-9]\d*$/;
      if (!regex.test(val[key])) {
        val[key] = ''
      } else {
        val[key] = val[key]
      }
    },

    handleSelect(val, list) {
      let obj = list.find(item => item.value === val.warnSonType)
      val.warnSonTypeStr = obj.label
    },

    handleMultiple(val) {
      val.earlyWarnDays = val.earlyWarn.join(',')
    },

    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.detailForm.validate((valid) => {
          if (valid) {
            resolve();
          } else {
            reject();
          }
        });
      });
    },


  },
};
</script>

<style lang="scss" scoped>
.box-card {
  margin-bottom: 15px;
}

.col-wrap {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  justify-content: flex-start;
}

.text-day {
  margin-left: 10px;
  font-size: 14px;
}

.el-form-item--small.el-form-item {
  margin-bottom: 0 !important;
}

.icon_add {
  cursor: pointer;
  margin-left: 10px;
}

</style>
